#from pylsl import StreamInlet, resolve_stream,local_clock
#from collections import deque
from PySide2.QtCore import  QRunnable,Signal,QObject
import os
from bs4 import BeautifulSoup
import requests
from requests.adapters import HTTPAdapter
import re
from bs4 import BeautifulSoup
import time
class downloadNovel(QRunnable):
    def __init__(self,url,novelName,savePath):
        super().__init__()
        self.Signals = preSignal()
        self.Url = url
        self.savePath = savePath
        self.novelName = novelName
        self.novelList = self.parse_page(self.Url)
        self.is_stop = False
        self.is_paused = False
        
       
    #获取页面
    def get_one_page(self,url):
        headers={
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'
                }
        session=requests.Session()
        session.mount('https://', HTTPAdapter(max_retries=3))
        session.mount('http://', HTTPAdapter(max_retries=3))    

        try:
            response= session.get(url,headers=headers,timeout=(5,20))
            response.encoding = 'utf-8'
            #print(response.status_code)
            if response.status_code==200:
                #print("get contents ok ! ")
                #print(response)
                return response.text
            else:
                print("爬取页面错误，返回代码为：%s"%response.status_code)
                self.Signals.error.emit("爬取页面错误，返回代码为：%s"%response.status_code)
        except requests.exceptions.RequestException as e:
            #print(e)
            self.Signals.error.emit("错误:%s"%e)

        return None
    #解析页面
    def parse_page(self,url):
        try:
            html=self.get_one_page(url)
            #html=url
            #link = re.search('<p>最新.*?<a href="(.*?)">(.*?)</a></p>', html, re.S)
            #正则表达式解析最新章节信息，返回章节名和章节链接
            soup = BeautifulSoup(html,"lxml")
            #返回最近十章的链接及章节名
            #Recently=soup.find_all(name="dl")
            NovelList=soup.find_all(name="dd")[12:]
            
            return NovelList 
        except Exception as e:
            self.Signals.error.emit("错误:%s"%e)

        
    
    def Stop(self):
        self.is_stop=True
    def Pause(self,state):
        self.is_paused=not self.is_paused
        if state =="暂停":
            self.Signals.pause.emit("下载过程被人为暂停。\n")
        else:
            self.Signals.pause.emit("下载继续。\n")
    def run(self):
        try:
            fileName = os.path.join(self.savePath,self.novelName+".txt")
            num=0
            with open(fileName,"w+",encoding="utf-8") as f:
                for chart in self.novelList:
                    #print(chart)
                    while self.is_paused:
                        time.sleep(0)
                    if self.is_stop==False:
                        if chart != "\n" and chart.name=='dd':
                            #print(chart)
                            title = chart.a.string
                            link = self.Url[:-10] + chart.a.attrs['href']
                            f.write(title+"\n")
                            html = self.get_one_page(link)
                            # 解析内容
                            soup = BeautifulSoup(html, "lxml")
                            # 返回最近十章的链接及章节名
                            content = soup.find("div",{"id":"content","class":"showtxt"})
                            text=content.text.replace("\r","\r\n").strip().replace(link,"").\
                               replace("请记住本书首发域名：www.shuquge.com。书趣阁_笔趣阁手机版阅读网址：m.shuquge.com","")+"\n"
                            #print(text)
                            f.write(text)
                            num += 1
                            message= "写入第%s/%s章《%s》 成功！\n"%(num,len(self.novelList),\
                                                             title)
                            self.Signals.progress.emit(message)
                            #self.Signals.count.emit(self.counter)
                    else:                   
                        self.Signals.stop.emit("下载过程被人为停止。\n")
                        break
            self.Signals.over.emit("下载结束!")
        except Exception as e:
            self.Signals.error.emit("错误:%s"%e)                  
                
            
                


class preSignal(QObject):    
    stop = Signal(str)
    over = Signal(str)
    pause =Signal(str)
    progress = Signal(str)
    error =Signal(str)
    